package com.example.om_export.app.report.service;

import com.example.om_export.app.report.dao.DocumentTemplateFieldMapper;
import com.example.om_export.app.report.entity.DocumentTemplateField;
import com.example.om_export.app.report.entity.ReportTemplate;
import com.example.om_export.common.entity.Params;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ReportService {
    @Autowired
    private DocumentTemplateFieldMapper dtMapper;
    public List<DocumentTemplateField> getDocumentTemplateList(){

        return dtMapper.selectAll(null);

    }

    public List<DocumentTemplateField> getTemplateFieldsByTemplateId(ReportTemplate template) {
        String[] ids = template.getTemplateFields().split(",");
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("ids",ids);
        List<DocumentTemplateField> list = dtMapper.selectAll(params);
        listSort(list,ids);
        return list;
    }

    /**
     * 排序
     * @param list
     * @param ids
     */
    private void listSort(List<DocumentTemplateField> list, String[] ids) {
        List<String> idLisit = Arrays.asList(ids);
        list.sort(Comparator.comparingInt(o->idLisit.indexOf(o.getId()+"")));
    }
}
